//
//  ViewControllerForPhotoPlayer.m
//  ProjectA
//
//  Created by dllo on 16/3/4.
//  Copyright © 2016年 刘旺辉. All rights reserved.
//

#import "ViewControllerForPhotoPlayer.h"
#import "CellForPhotoPlayer.h"
#import "ModelForDestinationPhoto.h"


@interface ViewControllerForPhotoPlayer ()<UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout>

@property (nonatomic, strong) NSMutableArray *arrForData;
@property (nonatomic, strong) NSIndexPath *indexPath;
@property (nonatomic, strong) UICollectionView *collectionView;

@end

@implementation ViewControllerForPhotoPlayer
- (void)dealloc {
    
    self.collectionView.dataSource = nil;
    self.collectionView.delegate = nil;
    
    [_arrForData release];
    [_indexPath release];
    [_collectionView release];
   

    [_url release];
    
    [super dealloc];
}

- (void)viewWillAppear:(BOOL)animated {
    [self.navigationItem.leftBarButtonItem setTintColor:L_COLOR_WHITE];
    self.barBackgroundView.backgroundColor = [UIColor blackColor];
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName:L_COLOR_WHITE}];
    
    
}
- (void)viewWillDisappear:(BOOL)animated {
    self.barBackgroundView.backgroundColor = [UIColor clearColor];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self settingsNavigationController];
    
    [self createCollectionView];
    
    
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    return CGSizeMake(self.view.bounds.size.width, self.view.bounds.size.height - 100);

}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
    return self.arrForData.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    CellForPhotoPlayer *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"pool" forIndexPath:indexPath];
    ModelForDestinationPhoto *model = [self.arrForData objectAtIndex:indexPath.item];
    [cell passData:model];
    
    return cell;
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
  
    NSInteger scroll = scrollView.contentOffset.x / self.view.bounds.size.width;
    ModelForDestinationPhoto *model = [self.arrForData objectAtIndex:scroll];
    self.navigationItem.title = model.trip_name;
}



- (void)createCollectionView {
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    flowLayout.minimumLineSpacing = 0;
    self.collectionView = [[UICollectionView alloc] initWithFrame:self.view.frame collectionViewLayout:flowLayout];
   
    [flowLayout release];
    [self.view addSubview:self.collectionView];
    [_collectionView release];
    
    self.collectionView.pagingEnabled = YES;
    
    self.collectionView.dataSource = self;
    self.collectionView.delegate = self;
    
    //http://api.breadtrip.com//destination/place/3/9990/photos/?gallery_mode=1&count=18&start=0
    [self.collectionView registerClass:[CellForPhotoPlayer class] forCellWithReuseIdentifier:@"pool"];
    
    [self.collectionView scrollToItemAtIndexPath:self.indexPath atScrollPosition:UICollectionViewScrollPositionRight animated:YES];
    ModelForDestinationPhoto *model = [self.arrForData objectAtIndex:self.indexPath.item];
    self.navigationItem.title = model.trip_name;
    
}
- (void)settingsNavigationController {
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"iconfont-fanhui(heise)"] style:UIBarButtonItemStylePlain target:self action:@selector(houtuiAction:)];
    
}
- (void)houtuiAction:(UIBarButtonItem *)item {
    
    [self.navigationController popViewControllerAnimated:YES];
    self.tabBarController.tabBar.hidden = YES;
    self.barBackgroundView.backgroundColor = [UIColor clearColor];
    
}
- (void)passData:(NSMutableArray *)arr indexPath:(NSIndexPath *)indexPath {
    self.arrForData = [NSMutableArray array];
    self.arrForData = arr;
    self.indexPath = indexPath;
    [self.collectionView reloadData];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
